//
//  XLTabBarController.m
//  dpapp
//
//  Created by zuoxueliangduyuanzhuo on 2017/5/31.
//  Copyright © 2017年 zuoxueliangduyuanzhuo. All rights reserved.
//

#import "XLTabBarController.h"
#import "XLNavigationVC.h"
#import "HomeVC.h"
#import "MeVC.h"
#import "OrderVC.h"

@interface XLTabBarController ()<UITabBarControllerDelegate>


@end

@implementation XLTabBarController


- (void)viewDidLoad {
    [super viewDidLoad];
    // 创建自定义tabbar
    [self addCustomTabBar];
    // 添加所有的子控制器
    [self addAllChildVcs];
    //观察购物车角标
    [self notifiCarNumber];
}

- (void)notifiCarNumber {
//    WEAK
//    [RACObserve([YBLUserManageCenter shareInstance], cartsCount) subscribeNext:^(id x) {
//        STRONG
//        [self.tabBar setBadgeValue:[YBLUserManageCenter shareInstance].cartsCount AtIndex:3];
//    }];
}

/**
 *  创建自定义tabbar
 */
- (void)addCustomTabBar
{
    self.delegate=self;
    // 创建自定义tabbar
    XLTabBar *customTabBar = [[XLTabBar alloc] init];
    customTabBar.backgroundColor = [UIColor whiteColor];
    // 更换系统自带的tabbar
    [self setValue:customTabBar forKeyPath:@"tabBar"];
}

/**
 *  添加所有的子控制器
 */
- (void)addAllChildVcs
{

    HomeVC *vc1 = [[HomeVC alloc] init];
    
    OrderVC *vc2 = [[OrderVC alloc] initWithNibName:@"XLBaseEmptyTableVC" bundle:nil];
    
    MeVC *vc3 = [[MeVC alloc] initWithNibName:@"XLBaseTableVC" bundle:nil];
    
    [self addOneChlildVc:vc1 title:@"" imageName:@"home_normal" selectedImageName:@"home_selected"];

    [self addOneChlildVc:vc2 title:@"" imageName:@"order_normal" selectedImageName:@"order_selected"];
    
    [self addOneChlildVc:vc3 title:@"" imageName:@"me_normal" selectedImageName:@"me_selected"];

}

/**
 *  添加一个子控制器
 *
 *  @param childVc           子控制器对象
 *  @param title             标题
 *  @param imageName         图标
 *  @param selectedImageName 选中的图标
 */
- (void)addOneChlildVc:(UIViewController *)childVc title:(NSString *)title imageName:(NSString *)imageName selectedImageName:(NSString *)selectedImageName {
    
    // 设置图标
    childVc.tabBarItem.image = [UIImage imageWithOriginal:imageName];
    // 设置选中的图标
    UIImage *selectedImage = [UIImage imageWithOriginal:selectedImageName];
    childVc.tabBarItem.selectedImage = selectedImage;
    
    //调整tabbarItem  图片的位置
    [childVc.tabBarItem setImageInsets:UIEdgeInsetsMake(6, 0, -6, 0)];
    
    // 添加为tabbar控制器的子控制器
    XLNavigationVC *nav = [[XLNavigationVC alloc] initWithRootViewController:childVc];
    [self addChildViewController:nav];
}

//禁止tab多次点击
-(BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController{
    //这里我判断的是当前点击的tabBarItem的标题
    
    return YES;
}

@end
